package com.ebay.services;

import com.ebay.util.DBconn;
import com.opensymphony.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class makePayment {

	Map session;
	private String seller_id, buyer_id, paisapay_acnt, category_id,
			subcategory_id, product_id, bank_name,accountno,custname,shipaddress,accntType;
	

	private float cost, balance, bill;
	int status, qnt, accnt_no;
	Connection con;
	Statement st;
	private String query;
	DBconn db;
	ResultSet rs;
    String date;
	public String updateAccount() {
		shipaddress="new town jamtara,jharkhand";
		session.put("shipaddress",shipaddress);
		accntType=(String) session.get("accntType");
		session.get("accntType");
        DateFormat df=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal=Calendar.getInstance();
		cal.add(cal.DATE,4);
		date = df.format(cal.getTime());
		session = ActionContext.getContext().getSession();
		setBuyer_id((String) session.get("userID"));
		setSeller_id((String) session.get("seller_id"));
		setCategory_id((String) session.get("category_id"));
		setSubcategory_id((String) session.get("subcategory_id") );
		setProduct_id((String) session.get("product_id"));
		setCost((Float) session.get("cost"));
		setQnt((Integer) session.get("qnt"));
		setShipaddress((String) session.get("shipaddress"));
		query = "insert into bank_third_party values('" + getCategory_id()
				+ "','" + getSubcategory_id() + "','" + getProduct_id() + "','"
				+ getBuyer_id() + "','" + getSeller_id() + "','" + getCost()
				+ "','" + getStatus() + "',' ','"+date+"')";
		db = new DBconn();

		if (0 == checkBalance()) {

			return "accountCheck";
		}
		con = db.getConnection();

		try {
			
			String query2="update bank_db set balance=balance-'"+cost+"' bank_acc_num='"
							+ getAccnt_no()
							+ "' and bank_name='"
							+ getBank_name() + "'";
			st = con.createStatement();
            int i=st.executeUpdate(query);
			System.out.print("update sttus is:"+i);
			i = st.executeUpdate(query2);
			System.out.print("update status is:"+i);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("makePayment"+e.getMessage());
			e.printStackTrace();
		}
		return "success";
	}

	public int checkBalance() {
		float bankOverflow;
		session = ActionContext.getContext().getSession();
		con = db.getConnection();

		try {
			st = con.createStatement();
			ResultSet rs = st
					.executeQuery("select balance from  bank_db where bank_acc_num='"
							+ getAccnt_no()
							+ "' and bank_name='"
							+ getBank_name() + "' and '"+getCustname()+"' and bank_acc_type='"+getAccntType()+"'");
			if (rs.next()) {
				if ((rs.getFloat("balance") < getBalance()) && getAccntType().equals("debit"))
					return 0;
				if ((rs.getFloat("balance") < getBalance()) && getAccntType().equals("credit")){
					bankOverflow=rs.getFloat("balance")+5000;//here 500 shows max credit given by bank
					if(bankOverflow < getBalance())
					return 0;
					else
						return 1;
					
				}
				else
					return 1;
			} else
				return 0;
		} catch (Exception e) {
         System.out.println("makePayment"+e.getMessage());
		}
		return 0;
	}

	
	public String getSeller_id() {
		return seller_id;
	}
	public String getShipaddress() {
		return shipaddress;
	}

	public String getAccntType() {
		return accntType;
	}

	public void setAccntType(String accntType) {
		this.accntType = accntType;
	}

	public void setShipaddress(String shipaddress) {
		this.shipaddress = shipaddress;
	}
	public void setSeller_id(String seller_id) {
		this.seller_id = seller_id;
	}

	public String getBuyer_id() {
		return buyer_id;
	}

	public void setBuyer_id(String buyer_id) {
		this.buyer_id = buyer_id;
	}

	public String getPaisapay_acnt() {
		return paisapay_acnt;
	}

	public void setPaisapay_acnt(String paisapay_acnt) {
		this.paisapay_acnt = paisapay_acnt;
	}

	public float getCost() {
		return cost;
	}

	public void setCost(float cost) {
		this.cost = cost;
	}

	public float getBalance() {
		return balance;
	}

	public String getAccountno() {
		return accountno;
	}

	public void setAccountno(String accountno) {
		this.accountno = accountno;
	}

	public String getCustname() {
		return custname;
	}

	public void setCustname(String custname) {
		this.custname = custname;
	}

	public void setBalance(float balance) {
		this.balance = balance;
	}

	public String getCategory_id() {
		return category_id;
	}

	public void setCategory_id(String category_id) {
		this.category_id = category_id;
	}

	public String getSubcategory_id() {
		return subcategory_id;
	}

	public void setSubcategory_id(String subcategory_id) {
		this.subcategory_id = subcategory_id;
	}

	public String getProduct_id() {
		return product_id;
	}

	public void setProduct_id(String product_id) {
		this.product_id = product_id;
	}

	public float getBill() {
		return bill;
	}

	public void setBill(float bill) {
		this.bill = bill;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getQnt() {
		return qnt;
	}

	public void setQnt(int qnt) {
		this.qnt = qnt;
	}

	public int getAccnt_no() {
		return accnt_no;
	}

	public void setAccnt_no(int accnt_no) {
		this.accnt_no = accnt_no;
	}

	public String getBank_name() {
		return bank_name;
	}

	public void setBank_name(String bank_name) {
		this.bank_name = bank_name;
	}
	// Iterator<cart> i = cartdetails.iterator();
	// while(i.hasNext()){
	// a = i.next();
	// seller_id=a.seller_id;
	// product_id=a.product_id;
	// buyer_id=a.buyer_id;
	// category_id=a.category_id;
	// subcategory_id=a.subcategory_id;
	// qnt=a.qnt;
	// cost=a.cost*qnt;
	// st.executeQuery(query);
	// bill=bill+cost;
	// }
}
